package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.model.Company;
import com.czd.support.model.Manager;
import com.czd.support.model.Navigate;
import com.czd.support.service.CompanyService;
import com.czd.support.service.NavigateService;

/**
 * @Description 企业信息导航
 * @Author JinMa.Xiao
 * @Date 2015年6月18日 下午4:56:11
 * @Version v1.0
 */
@Controller
@RequestMapping("/navigate")
public class NavigateController extends BaseController {

	@Autowired
	private NavigateService navigateService;
	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = { "", "/", "/list" })
	public String list(Model model, Navigate navigate,
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		navigate.setPageNo(pageNo);
		Paginate queryForPaginate = navigateService.queryForPaginate(navigate);
		model.addAttribute("paginate", queryForPaginate);
		return "navigate/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model  model) {
		List<Company> companyList = companyService.findAll();
		model.addAttribute("companyList", companyList);
		return "navigate/add";
	}

	@RequestMapping(value = "/{id}/add", method = RequestMethod.GET)
	public String addById(@PathVariable int id, Model model) {
		model.addAttribute("id", id);
		return "navigate/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest httpHttpServletRequest, Model model,
			Navigate navigate) {
		Manager sesionUser = this.getSesionUser(httpHttpServletRequest);
		if (sesionUser != null) {
			navigate.setCreateTime(sesionUser.getCreateTime());
			navigate.setCreateUser(sesionUser.getCreateUser());
			navigateService.insert(navigate);
			// 更新company导航图信息
			companyService.updateNav(navigate.getCompanyId());

			return "redirect:/navigate/list";
		} else {
			return "/login";
		}
	}

	@RequestMapping(value = "/{id}/del", method = RequestMethod.GET)
	public String del(@PathVariable int id) {
		navigateService.del(id);
		return "redirect:/navigate/list";
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model,
			HttpServletRequest httpServletRequest) {
		Navigate navigate = navigateService.findById(id);
		model.addAttribute("navigate", navigate);
		
		List<Company> companyList = companyService.findAll();
		model.addAttribute("companyList", companyList);
		return "navigate/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest httpServletRequest, Model modle,
			Navigate navigate) {
		Manager sesionUser = this.getSesionUser(httpServletRequest);
		if (sesionUser != null) {
			navigate.setUpdateUser(sesionUser.getUpdateUser() + "");
			navigate.setUpdateTime(sesionUser.getUpdateTime() + "");
			if (navigateService.update(navigate)) {
				return "redirect:/navigate/list";
			} else {
				return "/login";
			}
		}
		return "/login";
	}

	// public String details(){
	// return "" ;
	// }

}
